Clear["Gloab`*"];

fileSize[] := Module[{},
   (*统计文件或文件夹大小的函数*)
   fTotal[dirc_] := Module[{dir = dirc},
     If[TrueQ[FileType@dir == Directory],
      filesize = 
       FileSystemMap[FileSize, dir, Infinity, 1]~UnitConvert~"MB" // 
         Values // Total,
      filesize = (FileSize@dir)~UnitConvert~"MB"];
     
     (*将每一个文件（夹）大小打包为一个列表*)
     {dir, FileType@dir,
      If[TrueQ[Head@filesize == Quantity],
       filesize,
       Quantity[filesize, "Megabytes"]]}
     ];
   
   
   (*输入用户端的变量*)
   
   userpanel[] := Module[{},
     result = DialogInput[
       Grid[{
         {"输入路径: ", 
          InputField[Dynamic[userdir], String]}, {"输入需要展示的数据量: ", 
          InputField[Dynamic[amount], Number]},
         {DefaultButton[DialogReturn[{userdir, amount}]], 
          CancelButton[]}
         },
        Spacings -> {1, Automatic}, Alignment -> Left], Modal -> True
       ];
     Return[result]
     ];
   
   userpanel[];
   
   
   (*异常机制*)
   While[True,
    Which[
      DirectoryQ[userdir] == False,
      DialogInput[{"目录：" <> ToString@userdir <> " 不存在，请输入正确的目录", 
        DefaultButton[]}, Modal -> True];
      Clear@userdir;
      userpanel[],
      
      
      amount > Length@FileNames[All, userdir],
      DialogInput[{"文件总数量为" <> 
         ToString@Length@FileNames[All, userdir] <> 
         "，请重新输入一个小于该数量的正整数值", DefaultButton[]}, Modal -> True];
      Clear@amount;
      userpanel[],
      
      
      True, Break[];
      ];
    ];
   
   
   (*将结果展示表格*)
   newfilelist = ((fTotal /@ FileNames[All, userdir])~
       Sort~(#1[[3]] >= #2[[3]] &))[[;; amount]];
   Grid[
    Prepend[
     Flatten /@ (Riffle[Range@Length@newfilelist, newfilelist]~
        Partition~2), {"序号", "路径", "文件类型", "大小"}],
    Frame -> All, Alignment -> {{Left, Left, Left, Right}},
    Background -> {{3 -> LightGreen}, 
      None, {{1, 2} -> LightYellow, {1, 4} -> LightPink}}
    ]
   ];
